package com.cookor.poi.modules.phy.mobile;

import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.ResultUtil;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.utils.WorkDayUtils;
import com.cookor.poi.modules.phy.entity.TAction;
import com.cookor.poi.modules.phy.entity.TBookNo;
import com.cookor.poi.modules.phy.entity.TDoctor;
import com.cookor.poi.modules.phy.entity.TEvaluate;
import com.cookor.poi.modules.phy.entity.TFork;
import com.cookor.poi.modules.phy.entity.TStock;
import com.cookor.poi.modules.phy.service.TActionService;
import com.cookor.poi.modules.phy.service.TBookNoService;
import com.cookor.poi.modules.phy.service.TDoctorService;
import com.cookor.poi.modules.phy.service.TEvaluateService;
import com.cookor.poi.modules.phy.service.TForkService;
import com.cookor.poi.modules.phy.service.TStockService;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({"/api/phy/action"})
@ResponseBody
public class ApiActionController extends ApiBaseController {
  @Autowired
  private TActionService tActionService;
  
  @Autowired
  private TDoctorService tDoctorService;
  
  @Autowired
  private TBookNoService tBookNoService;
  
  @Autowired
  private TStockService tStockService;
  
  @Autowired
  private TForkService tForkService;
  
  @Autowired
  private TEvaluateService tEvaluateService;
  
  @RequestMapping({"/data"})
  public ResultUtil actionData(Integer actionId, String openId, HttpServletRequest request) {
    ResultUtil result = getResult();
    try {
      if (actionId == null || StringUtils.isBlank(openId))
        throw new Exception("参数有误"); 
      TAction tAction = this.tActionService.get(actionId.toString());
      TDoctor tDoctor = this.tDoctorService.get(tAction.getDoctorId().toString());
      result.putParams("doctor", tDoctor);
      tAction.setRule(tAction.getRule().replaceAll("/userfiles/", "https://zx.nthink.cn/userfiles/"));
      result.putParams("action", tAction);
      Calendar c = Calendar.getInstance();
      c.setTime(tAction.getActionDate());
      result.putParams("weekStr", WorkDayUtils.getChineseWeek(c));
      TStock stock = this.tStockService.getByParam(tAction.getTenantId(), actionId, tAction.getDoctorId(), Integer.valueOf(c.get(1)), 
          Integer.valueOf(c.get(2) + 1), Integer.valueOf(c.get(5)), Integer.valueOf(1));
      result.putParams("amStock", (stock == null) ? tAction.getAmStock() : stock.getStock());
      stock = this.tStockService.getByParam(tAction.getTenantId(), actionId, tAction.getDoctorId(), Integer.valueOf(c.get(1)), 
          Integer.valueOf(c.get(2) + 1), Integer.valueOf(c.get(5)), Integer.valueOf(2));
      result.putParams("pmStock", (stock == null) ? tAction.getPmStock() : stock.getStock());
      TFork fork = this.tForkService.findByParam(openId, tAction.getDoctorId());
      result.putParams("fork", Integer.valueOf((fork == null) ? 0 : fork.getStatus().intValue()));
      TEvaluate tEvaluate = new TEvaluate();
      tEvaluate.setDelFlag("0");
      tEvaluate.setDoctorId(Integer.valueOf(tDoctor.getId()));
      tEvaluate.setTenantId(tAction.getTenantId());
      List<TEvaluate> evaList = this.tEvaluateService.findList(tEvaluate);
      int good = 0;
      int bad = 0;
      for (TEvaluate eva : evaList) {
        if (eva.getStar().intValue() >= 3) {
          good++;
          continue;
        } 
        bad++;
      } 
      result.putParams("evaStatus", (bad == 0) ? Integer.valueOf(100) : (new BigDecimal(good)).divide(new BigDecimal(good + bad)).setScale(2, RoundingMode.UP).multiply(new BigDecimal(100)));
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("WxActionController actionList error!", e);
    } 
    return result;
  }
  
  @RequestMapping({"/bookNo"})
  public ResultUtil actionBookNo(Integer actionId, Integer type, Integer siteid, HttpServletRequest request) {
    ResultUtil result = getResult();
    try {
      if (actionId == null || type == null)
        throw new Exception("参数有误"); 
      List<TBookNo> bookNoList = this.tBookNoService.queryByAppoint(siteid, actionId, null, null, null, null, type, 
          Integer.valueOf(0));
      result.putParams("schedule", bookNoList);
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("WxActionController actionList error!", e);
    } 
    return result;
  }
  
  @RequestMapping({"/list"})
  public ResultUtil actionList(Integer pageSize, Integer pageNo, Integer queryTenantId, Integer officeId, String doctorName, HttpServletRequest request) {
    ResultUtil result = getResult();
    try {
      Integer tenantId = getTenantId(request);
      if (pageNo == null)
        pageNo = Integer.valueOf(1); 
      if (pageSize == null)
        pageSize = Integer.valueOf(-1); 
      TAction tAction = new TAction();
      tAction.setTenantId(queryTenantId);
      tAction.setOfficeId(officeId);
      tAction.setQueryBeginDateUp(new Date());
      tAction.setQueryEndDateDown(new Date());
      tAction.setDoctorName(doctorName);
      tAction.setDelFlag("0");
      result.putParams("page", this.tActionService.findPage(new Page(pageNo.intValue(), pageSize.intValue()), tAction));
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("WxActionController actionList error!", e);
    } 
    return result;
  }
  
  @RequestMapping({"/all/list"})
  public ResultUtil allList(Integer pageSize, Integer pageNo, String queryName, Integer queryType, HttpServletRequest request) {
    ResultUtil result = getResult();
    try {
      if (pageNo == null)
        pageNo = Integer.valueOf(1); 
      if (pageSize == null)
        pageSize = Integer.valueOf(5); 
      TAction tAction = new TAction();
      if (queryType == null)
        throw new Exception("参数异常"); 
      LocalDate today = LocalDate.now();
      if (queryType.intValue() == 2)
        today.plusDays(7L); 
      Date monday = Date.from(today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).atStartOfDay(ZoneId.systemDefault()).toInstant());
      Date sunday = Date.from(today.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY)).atStartOfDay(ZoneId.systemDefault()).toInstant());
      if (queryType.intValue() == 1 || queryType.intValue() == 2) {
        tAction.setQueryActionDateDown(monday);
        tAction.setQueryActionDateUp(sunday);
      } else if (queryType.intValue() == 3) {
        tAction.setQueryActionDateUp(new Date());
      } 
      tAction.setQueryName(queryName);
      this.tActionService.findPage(new Page(pageNo.intValue(), pageSize.intValue()), tAction);
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("WxActionController actionList error!", e);
    } 
    return result;
  }
}
